package api

import (
	__ "api-gateway/basic/activity_proto"
	"api-gateway/basic/inits"
	"api-gateway/handler/request"
	"api-gateway/handler/response"
	"github.com/gin-gonic/gin"
)

// CreateActivity 创建秒杀活动（秒杀前）
// @Summary 创建秒杀活动（秒杀前）
// @Description 创建秒杀活动（秒杀前）
// @Tags 秒杀
// @Accept json
// @Produce json
// @Param req body request.CreateActivity true "创建秒杀活动（秒杀前）"
// @Success 200 {object} response.Response{data=string} "创建秒杀活动（秒杀前）"
// @Router /seckill [post]
func CreateActivity(c *gin.Context) {
	var req request.CreateActivity
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	activity, err := inits.ActivityClient.CreateActivity(c, &__.CreateActivityReq{
		GoodsId:        req.GoodsId,
		UserId:         req.UserId,
		ActivityTitle:  req.ActivityTitle,
		Info:           req.Info,
		Price:          req.Price,
		SeckillPrice:   req.SeckillPrice,
		Stock:          req.Stock,
		GoodsStatus:    req.GoodsStatus,
		StartTime:      req.StartTime,
		EndTime:        req.EndTime,
		Num:            req.Num,
		ActivityStatus: req.ActivityStatus,
	})
	if err != nil {
		response.Error(c, 500, "创建活动失败", err.Error())
		return
	}
	response.Success(c, "创建活动成功", activity.Id)
}

// ExecuteActivity 执行秒杀活动（秒杀中）
func ExecuteActivity(c *gin.Context) {
	var req request.ExecuteActivity
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	activity, err := inits.ActivityClient.ExecuteActivity(c, &__.ExecuteActivityReq{
		ActivityId: req.ActivityId,
		UserId:     req.UserId,
	})
	if err != nil {
		response.Error(c, 500, "执行活动失败", err.Error())
		return
	}

	response.Success(c, "执行活动成功", activity)
}

// 完成秒杀活动（秒杀后）
func FinishActivity(c *gin.Context) {
	var req request.FinishActivity
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	activity, err := inits.ActivityClient.FinishActivity(c, &__.FinishActivityReq{
		GoodsId:    req.GoodsId,
		ActivityId: int64(req.ActivityId),
	})
	if err != nil {
		response.Error(c, 500, "完成活动失败", err.Error())
		return
	}
	response.Success(c, "完成活动成功", activity)
}

// 展示活动列表
func ListActivity(c *gin.Context) {
	var req request.ListActivity
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	activity, err := inits.ActivityClient.ListActivity(c, &__.ListActivityReq{
		Id: req.Id,
	})
	if err != nil {
		response.Error(c, 500, "展示活动列表失败", err.Error())
		return
	}
	response.Success(c, "展示活动列表成功", activity)
}
